package com.jianyun.jyzs.widget;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.CrmActivity;


/**
 * Created by mlli on 2017/8/30.
 */

public class CrmTimeSearch {
    private CrmActivity context;
    private CheckBox appCrmSearchTimeText;
    private PopupWindow popupWindow;
    private boolean order = true;

    public CrmTimeSearch(CrmActivity context, CheckBox appCrmSearchTimeText) {
        this.context = context;
        this.appCrmSearchTimeText = appCrmSearchTimeText;
    }
    public void show(View drop){
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_crm_search_time, null);
        final LinearLayout createTimeLinearLay = (LinearLayout)view.findViewById(R.id.createTimeLinearLay);
        final LinearLayout updateTimeLinearLay = (LinearLayout)view.findViewById(R.id.updateTimeLinearLay);
        final TextView createTime = (TextView)view.findViewById(R.id.createTime);
        final TextView createTimeOrder = (TextView)view.findViewById(R.id.createTimeOrder);
        final ImageView createTimeCheck = (ImageView)view.findViewById(R.id.createTimeCheck);
        final TextView updateTime = (TextView)view.findViewById(R.id.updateTime);
        final ImageView updateTimeCheck = (ImageView)view.findViewById(R.id.updateTimeCheck);
        final TextView updateTimeOrder = (TextView)view.findViewById(R.id.updateTimeOrder);
        if (order){
            createTimeCheck.setImageDrawable(context.getResources().getDrawable(R.drawable.rc_ad_list_file_checked));
        }else {
            updateTimeCheck.setImageDrawable(context.getResources().getDrawable(R.drawable.rc_ad_list_file_checked));
        }

        view.findViewById(R.id.disclick).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
            }
        });
        popupWindow = new Solve7PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        createTimeLinearLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createTimeLinearLay.setBackgroundColor(context.getResources().getColor(R.color.bg));
                createTime.setTextColor(context.getResources().getColor(R.color.blue));
                createTimeOrder.setTextColor(context.getResources().getColor(R.color.blue));
                createTimeCheck.setImageDrawable(context.getResources().getDrawable(R.drawable.rc_ad_list_file_checked));
                context.searchTime(false);
                order = true;
                popupWindow.dismiss();
            }
        });
        updateTimeLinearLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateTimeLinearLay.setBackgroundColor(context.getResources().getColor(R.color.bg));
                updateTime.setTextColor(context.getResources().getColor(R.color.blue));
                updateTimeOrder.setTextColor(context.getResources().getColor(R.color.blue));
                updateTimeCheck.setImageDrawable(context.getResources().getDrawable(R.drawable.rc_ad_list_file_checked));
                context.searchTime(true);
                order = false;
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0x4D000000));
        popupWindow.update();
        popupWindow.showAsDropDown(drop);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                appCrmSearchTimeText.setChecked(false);
            }
        });
    }

    public void diss() {
        popupWindow.dismiss();
        appCrmSearchTimeText.setChecked(false);
    }
}
